<?php

namespace app\index\controller;

use think\Controller;
use think\Db;
use Myredis\Myredis;
use think\Request;
use think\Session;
class Index extends Controller
{
    public function index()
    {


        if (Request::instance()->session('tel')) {
            $tel = Request::instance()->session('tel');
            $res = Db::table('xo_user')->field('pwd', true)->where('tel', $tel)->find();
            Session::set('name', $res['nickname']);
            Session::set('uid', $res['id']);
            Session::set('icon', $res['icon']);
        }
        $stoken = Request::instance()->session('token');
        $rtoken = MyRedis::get(Request::instance()->session('tel') . 'token');
        $etoken =MyRedis::get(Request::instance()->session('email').'token');
        if (!empty($rtoken)) {
            if ($stoken != $rtoken) {
                Session::clear();
                return $this->success('账号异常登录,请注意账号安全', url('index/index/index'));
            }
        }
        if (!empty($etoken)) {
            if ($stoken != $etoken) {
                Session::clear();
                return $this->success('账号异常登录,请注意账号安全', url('index/index/index'));
            }
        }
        $info = Myredis::get('index_info');
        if (empty($info)) {
            $list = Db::table('xo_goods_up')->field(true)->order('sold', 'desc')->limit('8')->select();
            $res = Db::table('xo_admin_banner')->field(['icon','url'])->where(['status' => '1'])->limit('3')->select();
            $row2 = Db::table('xo_goods_up')->field(true)->where(['row' => '2'])->select();
            $row3 = Db::table('xo_goods_up')->field(true)->where(['row' => '3'])->select();
            $row4 = Db::table('xo_goods_up')->field(true)->where(['row' => '4'])->select();
            $row5 = Db::table('xo_goods_up')->field(true)->where(['row' => '5'])->select();
            $row6 = Db::table('xo_goods_up')->field(true)->where(['row' => '6'])->select();

            $info['list'] = $list;
            $info['res'] = $res;
            $info['row2'] = $row2;
            $info['row3'] = $row3;
            $info['row4'] = $row4;
            $info['row5'] = $row5;
            $info['row6'] = $row6;
            Myredis::setex('index_info', 600, $info);
        }
        $list = $info['list'];
        $res = $info['res'];
        $row2 = $info['row2'];
        $row3 = $info['row3'];
        $row4 = $info['row4'];
        $row5 = $info['row5'];
        $row6 = $info['row6'];

        if (empty($res[0]['icon'])) {
            $pic1 = '';
        } else {
            $pic1['icon'] = 'http://pdyc9epam.bkt.clouddn.com/' . $res[0]['icon'];
            $pic1['url'] =$res[0]['url'];

        }
        if (empty($res[1]['icon'])) {
            $pic2 = '';
        } else {
            $pic2['icon'] = 'http://pdyc9epam.bkt.clouddn.com/' . $res[1]['icon'];
            $pic2['url'] = $res[1]['url'];
        }
        if (empty($res[2]['icon'])) {
            $pic3 = '';
        } else {
            $pic3['icon'] = 'http://pdyc9epam.bkt.clouddn.com/' . $res[2]['icon'];
            $pic3['url'] =  $res[2]['url'];
        }
        $data = Db::table('xo_admin_category')->field(['id', 'name','pid'])->where(['pid' => '0','display'=>'1'])->select();

        if(empty($data)){
            return view('index', ['list' => $list, 'row2' => $row2, 'row3' => $row3, 'row4' => $row4, 'row5' => $row5, 'pic1' => $pic1, 'pic2' => $pic2, 'pic3' => $pic3,'data'=>$data,'row6' => $row6]);
        }
        //一级分类
        foreach ($data as $key => $val) {
            $arr['id'][] = $val['id'];
        }
        $cate=array();
        //二级分类
        foreach ($arr['id'] as $key => $val) {
            $res = Db::table('xo_admin_category')->field(['id', 'name','pid'])->where(['pid' => $val])->select();
            if (!empty($res)) {
                $cate[$val] = $res;
            }
        }

        $arr1=array();
        foreach ($cate as $key => $val) {
            foreach ($val as $k => $v) {
                $arr1[$v['id']]['id'] = $v['id'];
                $arr1[$v['id']]['name'] = $v['name'];
            }
        }

        $arr2=array();

        foreach ($arr1 as $key => $val) {
            $res = Db::table('xo_admin_category')->field(['id', 'name','pid'])->where(['pid' => $val['id']])->select();

            if (!empty($res)) {
                $arr2[$val['id']] = $res;
            }
        }
        $adv=Db::table('xo_advertisement')->field(true)->where(['status'=>1])->select();
        $num = count($adv);
        return view('index', ['list' => $list, 'row2' => $row2, 'row3' => $row3, 'row4' => $row4, 'row5' => $row5,'row6' => $row6, 'pic1' => $pic1, 'pic2' => $pic2, 'pic3' => $pic3, 'data' => $data, 'cate' => $cate, 'arr2' => $arr2,'adv'=>$adv,'num'=>$num]);
    }

    public function join()
    {

        return view('index/join');
    }


    public function cate1()
    {
        $data = Db::table('xo_admin_category')->field(['id', 'name','pid'])->where(['pid' => '0'])->select();
        if(empty(input('id'))){
            //一级分类
            foreach ($data as $key => $val) {

                $arr[$key]['id'] = $val['id'];
                $arr[$key]['name'] = $val['name'];
            }
            $goods=Db::table('xo_goods_up')->field(true)->paginate(16);

            $page=$goods->render();
            $count=Db::table('xo_goods_up')->field('id')->count();
            return view('index/cate1',['data'=>$goods,'name'=>$arr,'count'=>$count,'page'=>$page,'cate'=>$data,'cate1'=>'0','cate2'=>'0']);
        }else{
            $id=input('id');
            $pid=input('pid');
            //遍历变量ID 的分类名和宝贝名
            $res = Db::table('xo_admin_category')->field(['id', 'name','pid'])->where(['pid' => $id])->select();
            if(empty($pid)){

                $cate1=$res;
                $cate2='0';
                $str=$id.',';
                //找出来所有PATH中带$STR的宝贝
//                foreach($res as $k =>$v)
//                {
//                $str.=$v['id'].',';
//                }
//                查出所有PATH中带有$str的分类
                $path=Db::table('xo_admin_category')->field(['id','name'])->where('path','like','%'.$str.'%')->select();

                $s='';
                foreach($path as $k =>$v)
                {
                $s.=$v['id'].',';
                }
                $s=rtrim($s,',');
                $map['cid']=array('in',$s);
                $goods= Db::table('xo_goods_up')->field(true)->where($map)->paginate(16);
                $page = $goods->render();
                return view('index/cate1',['data'=>$goods,'name'=>$res,'page'=>$page,'cate'=>$data,'cate1'=>$cate1,'cate2'=>$cate2]);
            }else{

                $cate1=Db::table('xo_admin_category')->field(['id','name','pid'])->where(['id'=>$id])->select();
                //查看该分类的下一级分类
                $cate2=Db::table('xo_admin_category')->field(['id','name','pid'])->where(['pid'=>$id])->select();
                //如果$cate2的内容为空 那么$cate2就等于
                //上一级ID是14
//                if(empty($cate2)){
//                    $cate1=Db::table('xo_admin_category')->field(['id','name','pid'])->where(['id'=>$pid])->select();
//                    $cate2=Db::table('xo_admin_category')->field(['id','name','pid'])->where(['pid'=>$pid])->select();
//                }
            }

            $arr=array();
            foreach ($res as $key => $val) {
                $arr[$key]['id'] = $val['id'];
                $arr[$key]['name'] = $val['name'];
            }
            $name=$arr;
            $count=Db::table('xo_goods_up')->field('id')->where(['cid'=>$id])->count();

            $str=$id.',';
//            找出来所有PATH中带$STR的宝贝
                foreach($res as $k =>$v)
                {
                $str.=$v['id'].',';
                }
//                查出所有PATH中带有$str的分类
            $path=Db::table('xo_admin_category')->field(['id','name'])->where('path','like',$str.'%')->select();

            if(!empty($path)){
                $s='';
                foreach($path as $k =>$v)
                {
                    $s.=$v['id'].',';
                }
                $s=rtrim($s,',');
            }else{
                $s=rtrim($str,',');
            }


            $map['cid']=array('in',$s);
            $goods= Db::table('xo_goods_up')->field(true)->where($map)->paginate(16);
            $page = $goods->render();
            return view('index/cate1',['data'=>$goods,'name'=>$name,'count'=>$count,'page'=>$page,'cate'=>$data,'cate1'=>$cate1,'cate2'=>$cate2]);
        }
    }


    public function search()
    {
        $str = input('str');
        if (empty($str)) {
            return $this->error('您还没有输入搜索内容哦');
        }


//

        $data = Db::table('xo_goods_up')->field(true)->where(['pname' => ['like', $str], 'check' => '1'])->paginate(12);
        $page = $data->render();
        return view('index/search', ['data' => $data, 'page' => $page, 'str' => $str]);
    }


}
